const app = getApp()
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()
Component({
      data: {
            selected: 0,
            color: "#000",
            selectedColor: "#000",
            list: [{
                  index: 0,
                  pagePath: "/pages/index/index",
                  iconPath: "/icon/select-home.png",
                  selectedIconPath: "/icon/home.png",
                  text: "首页"
            }, {
                  index: 1,
                  pagePath: "/pages/home/home",
                  iconPath: "/icon/select-user.png",
                  selectedIconPath: "/icon/user.png",
                  text: "我的"
            }],
            showBg: 'show'
      },

      methods: {
            switchTab(e) {
                  const data = e.currentTarget.dataset
                  const url = data.path
                  wx.switchTab({ url })
                  getApp().globalData.index = data.index;
            },
            /**按下则触发 */
            tapSpeech() {
                  wx.showLoading({
                        title: '请稍等',
                  })
                  const that = this;
                  // this.stopSpeech();
                  manager.onStart = function (res) {
                        console.log('onstart');
                        that.setData({
                              showBg: 'no'
                        })
                        wx.showToast({
                              title: '正在录音',
                              image: '/icon/mike.png',
                              duration: 30000,
                              mask: false
                        })
                  }
                  manager.onError = function (res) {
                        console.error("error msg", res.msg)
                        wx.hideLoading()
                  }
                  this.stopSpeech();
                  manager.start({
                        lang: 'zh_CN',
                        duration: 30000
                  })
            },
            /**松开按钮 */
            endSpeech() {
                  wx.showLoading({
                        title: '识别中'
                  });
                  this.setData({
                        showBg: 'show'
                  })
                  manager.stop();
                  this.stopSpeech();

            },
            cancel() {
                  manager.stop();
                  this.setData({
                        showBg: 'show'
                  })
                  wx.showToast({
                    title: '结束录制',
                    duration: 2000
                  })
            },
            stopSpeech() {
                  manager.onStop = function (res) {
                        if (res.result == '') {
                              wx.showToast({
                                    title: '未识别到内容',
                                    icon:'error'
                              })
                        } else if (res.result.length < 2) {
                              wx.showToast({
                                    title: '未识别到内容',
                                    icon:'error'
                              })
                        } else {
                              wx.navigateTo({
                                    url: '/pages/search/search?val=' + res.result,
                              })
                        }
                        wx.hideLoading();
                  }
            }

      }
})